//
//  BasicInformationViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/9/1.
//

#import "BasicInformationViewController.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookRadioCell.h"
#import "CoachsBookAvatarCell.h"
#import "LongUIImagePickerController.h"
@interface BasicInformationViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextFieldDelegate>

@end

@implementation BasicInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navgationheadv.titlelabel.text = @"提交认证";
    self.navgationheadv.rightbtn.hidden = NO;
    self.navgationheadv.rightbtn.frame = CGRectMake(APPwidth-80, iPhoneXBool?SafeAreaTopHight/2+10:SafeAreaTopHight/2-5, 60, 20);
    [self.navgationheadv.rightbtn setTitle:@"认证证明" forState:0];
    [self.navgationheadv.rightbtn setTitleColor:rgba(22, 120, 255, 1) forState:0];
    [self.navgationheadv.rightbtn addTarget:self action:@selector(helpBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];
    titlearr = @[@"头像",@"姓名",@"城市",@"驾校",@"性别",@"微信号",@"邮箱",@"入行教练时间",@"教龄",@"训练场信息",@"个人简介",@"教学环境",@"身份",@"教学科目"];
    promptarr = @[@"",@"请输入姓名",@"请选择城市",@"请选择驾校",@"",@"请填写微信号",@"请填写邮箱",@"请选择入行时间",@"",@"",@"请输入个人简介",@"未上传",@"",@"请选择"];
    celltypearr = @[@"9",@"1",@"1",@"1",@"2",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"2",@"1"];
    for (NSInteger i=0; i<titlearr.count; i++) {
        CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
        model.title = titlearr[i];
        model.prompt = promptarr[i];
        model.cellType = [celltypearr[i] integerValue];
        model.showArrow = NO;
        if (i==0||i==2||i==3||i==7||i==9||i==10||i==11||i==13) {
            model.showArrow = YES;
        }
        else if (i==4)
        {
            model.leftTitle = @"男";
            model.righTitle = @"女";
            model.content = @"1";
        }
        else if (i==12)
        {
            model.leftTitle = @"教练";
            model.righTitle = @"招生";
            model.content = @"1";
        }
        [self.dataArray addObject:model];
    }

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight);
    
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    //招生服务
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"基本信息(必填)";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"基本信息(必填)"]];
    [str addAttribute:NSForegroundColorAttributeName value:rgba(255, 59, 59, 1) range:NSMakeRange(4,4)];
    myDataLab.attributedText = str;
    
    self.tableView.tableHeaderView = HeaderV;
    
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CoachsBookInputModel *model = self.dataArray[indexPath.row];
    
    if (model.cellType == 1) {
        CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
        if (!cell) {
            cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.contentField.tag = indexPath.row;
        cell.contentField.delegate = self;
        return cell;
    }
    else if (model.cellType == 2)
    {
        CoachsBookRadioCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookRadioCell"];
        if (!cell) {
            cell=[[CoachsBookRadioCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookRadioCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model AndleftTitle:model.leftTitle righTitle:model.righTitle];
        [cell.lefttabbut addTarget:self action:@selector(lefttabbutCheng:) forControlEvents:UIControlEventTouchUpInside];
        [cell.righttabbut addTarget:self action:@selector(righttabbutCheng:) forControlEvents:UIControlEventTouchUpInside];
        cell.lefttabbut.tag = cell.righttabbut.tag = indexPath.row;
        return cell;
    }

    CoachsBookAvatarCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookAvatarCell"];
    if (!cell) {
        cell=[[CoachsBookAvatarCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookAvatarCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)lefttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"1";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)righttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"2";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}



#pragma mark - 拍照选择图片
-(void)photoImagetapCheng:(UITapGestureRecognizer *)tap
{
    UIView *view = tap.view;
    NSLog(@"点击了第%ld个view",view.tag);
    [LongHelper poppingBottomAlertWithReminderTitle:nil message:nil AndOneActionTitle:@"拍照" OneActionHandler:^(UIAlertAction *action)
     {
        [self takePhotoWithTag:view.tag];
     } AndTwoActionTitle:@"从手机相册选择" TwoActionHandler:^(UIAlertAction *action) {
         [self selectPhotoWithTag:view.tag];
     } AndCancelActionTitle:@"取消" NoActionHandler:^(UIAlertAction *action) {
         
     } ViewController:self];
}


#pragma mark - 相机拍照
- (void)takePhotoWithTag:(NSInteger)tag
{
    LongUIImagePickerController *imagePickerController = [[LongUIImagePickerController alloc]init];
    imagePickerController.tag = tag;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;    //设置来源为相机
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;  //设置后置摄像头
    imagePickerController.delegate = self;
//    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
}


#pragma mark - 相册选择相片
- (void)selectPhotoWithTag:(NSInteger)tag
{
    LongUIImagePickerController *imagePickerController = [[LongUIImagePickerController alloc]init];
    imagePickerController.tag = tag;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    //设置来源为相册
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    LongUIImagePickerController *imagePickerController = (LongUIImagePickerController*)picker;
    NSLog(@"点击了第%ld个imagePickerController",imagePickerController.tag);
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CoachsBookInputModel *model = self.dataArray.lastObject;
    if (imagePickerController.tag == 1) {
        model.idCardfacephotoimage = image;
        model.idCardfacephotourl = @"";
    }
    else if (imagePickerController.tag == 2)
    {
        model.idCardbadgephotoimage = image;
        model.idCardbadgephotourl = @"";
    }
    else if (imagePickerController.tag == 3)
    {
        model.driverlicensephotoimage = image;
        model.driverlicensephotourl = @"";
    }
    else
    {
        model.avatarphotoimage = image;
        model.avatarphotourl = @"";
    }
    [self.tableView reloadData];
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        

        
        if (textField.tag == 1) {
//            TrainingViewController *vc = [[TrainingViewController alloc]init];
//            [self.view endEditing:YES];
//            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (textField.tag == 2)
        {
//            TrainingViewController *vc = [[TrainingViewController alloc]init];
//            vc.fromVC = self;
//            vc.setTrainingBlock = ^(NSString * _Nonnull TrainingName) {
//                
//            };
//            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}




- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}







-(void)helpBtnClick
{
    [self.view endEditing:YES];
}

@end
